package com.ask.service.asyncService;

import com.ask.entity.Orders;
import com.ask.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class OrderAsyncService {
    @Autowired
    private OrderMapper orderMapper;

    @Async("orderTaskExecutor") // 标记该方法应异步执行，并指定使用名为orderTaskExecutor的线程池
    public void asyncProcessOrder(Orders order) {
        try {
            log.info("超时订单取消，orderId:{}：", order.getId());
            order.setStatus(Orders.CANCELLED);
            order.setCancelReason("订单超时，自动取消");
            order.setCancelTime(LocalDateTime.now());
            orderMapper.updateCanceledOrder(order);
        } catch (Exception e) {
            log.error("处理过期订单失败, orderId:{}", order.getId(), e);
        }
    }
}
